//
//  AppDelegate.m
//  GOGOTravel
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "AppDelegate.h"
#import "RecommendViewController.h"
#import "CityHunterViewController.h"
#import "DestinationViewController.h"
#import "UserViewController.h"
#import "DengLuView.h"
#import "ZHONGJIANViewController.h"
#import "LunchingViewController.h"
#import "UMSocial.h"
#import "LTIntroductionViewController.h"
@interface AppDelegate ()<DengLuViewDelegate>

@property (nonatomic, retain) DengLuView *DLV;
@property (nonatomic, retain) UITabBarController *recommendTabBar;
@property (nonatomic, retain) UIImageView *image2;

/** 动画 */
@property (nonatomic, assign) BOOL isYES;
@property (nonatomic, retain) UIView *animationView;
@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIView *animationView1;
@property (nonatomic, retain) UIImageView *imageV1;
@property (nonatomic, retain) UILabel *label1;
@property (nonatomic, retain) UIView *backView;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, assign) BOOL isDengLu;
@property (nonatomic, retain) Reachability *conn; // 检查网络状态对象;
@property (nonatomic, retain) LTIntroductionViewController *linDaoYe;
@end
@implementation AppDelegate


- (void)dealloc{

    [_DLV release];
    [_recommendTabBar release];
    [_image2 release];
    [_animationView release];
    [_animationView1 release];
    [_imageV release];
    [_imageV1 release];
    [_imageView release];
    [_label release];
    [_label1 release];
    [_backView release];
    [_button release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
   
    
    [UMSocialData setAppKey:@"56d6f3dee0f55a29fb0029fd"];
    
    
    
    LunchingViewController *lunch = [[LunchingViewController alloc] init];
    
    
    
    
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [_window release];
    
    //创建推荐tabbar
    
    _window.rootViewController = lunch;
    
    [lunch release];
    
    RecommendViewController *recommemd = [[RecommendViewController alloc] init];
    
    UINavigationController *recommendNav = [[UINavigationController alloc] initWithRootViewController:recommemd];
    
    
    
    UIImage *recommendImageTwo = [UIImage imageNamed:@"iconfont-lvxing.png"];
    
    
    recommendNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"游推荐" image:recommendImageTwo tag:1008];
    
    //创建城市猎人tabbar
    
    CityHunterViewController *cityH = [[CityHunterViewController alloc] init];
    
    UINavigationController *cityNav = [[UINavigationController alloc] initWithRootViewController:cityH];
    [self setNav];
    
    
    UIImage *cityImage = [UIImage imageNamed:@"iconfont-yundongshenjing-2.png"];
    
    cityH.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"游猎人" image:cityImage tag:1005];
    
    DestinationViewController *des = [[DestinationViewController alloc] init];
    
    UINavigationController *nacDes = [[UINavigationController alloc] initWithRootViewController:des];
    UIImage *image = [UIImage imageNamed:@"iconfont-camera_line.png"];
    
    nacDes.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"想去哪" image:image tag:1000];
    
    UserViewController *userVc = [[UserViewController alloc] init];
    
    UINavigationController *userNav = [[UINavigationController alloc] initWithRootViewController:userVc];
    
    UIImage *imageUser = [UIImage imageNamed:@"iconfont-wode.png"];
    
    userNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:imageUser tag:1009];
    
    self.recommendTabBar = [[UITabBarController alloc] init];
    
    _recommendTabBar.viewControllers = @[nacDes, recommendNav,cityNav, userNav];
    _recommendTabBar.tabBarController.tabBar.barStyle = UIBarStyleBlackOpaque;
    _recommendTabBar.tabBarController.tabBar.barTintColor = UIColorFromRGB(0x099cc);
    _recommendTabBar.tabBar.barTintColor = [UIColor whiteColor];
    _window.rootViewController = _recommendTabBar;
    
    /**<  引导页 */
    
    NSString *firstLog = [[NSUserDefaults standardUserDefaults] objectForKey:@"firstLog"];
    
    if (![firstLog isEqualToString:@"yes"]) {
        NSArray *imageArr = @[@"1", @"2", @"3"];
        NSArray *backImageArr = @[@"1", @"2", @"3"];
        
        
        [[NSUserDefaults standardUserDefaults] setObject:@"isDay" forKey:@"yeJianMoShi"];
        self.linDaoYe = [[LTIntroductionViewController alloc] initWithCoverImageNames:imageArr backgroundImageNames:backImageArr];
        
        [self.window addSubview:_linDaoYe.view];
        [_linDaoYe enterButton];
        
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"firstLog"];
        
        }
    
    
    _linDaoYe.didSelectedEnter = ^(){
        [_linDaoYe.view removeFromSuperview];
        _linDaoYe = nil;
    };
    
    
    
    
    
    
    
    
    
  /**<  睡眠时间 */
    sleep(2);
    
    /** 数据库打开 */
    [[DbHandel shareDbHandel] openDB];
    [[DbHandel shareDbHandel] createTable];
    
    [[DbHandel shareDbHandel] createDengLuTable];
    
    [[DbHandel shareDbHandel] createDailySCTable];
    
    
    //登陆数据库
    NSArray *arr = [[DbHandel shareDbHandel] selectDengLuTable];
   
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dengluCGAction:) name:@"denglucs" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DengLuAction:) name:@"去登陆" object:nil];
    
    // 通知中心 判断网络状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetWorkState) name:kReachabilityChangedNotification object:nil];
    self.conn = [Reachability reachabilityForInternetConnection];
    // 让Reachability开启被监听状态
    [self.conn startNotifier];
    [self checkNetWorkState];
    
    
    _isDengLu = YES;
    
    return YES;
}
/**<  网络状态alert */
- (void)checkNetWorkState {
    // 1.检测wifi状态
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    // 2.检测手机能否上网
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    
    // 有wifi状态
    if ([wifi currentReachabilityStatus] != NotReachable) {
        
        
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"通知" message:@"当前为wifi状态" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
                [alertView release];
        
        // 手机网络状态
    }else if ([conn currentReachabilityStatus] != NotReachable){
        
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"当前为手机网络状态" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
                [alertView release];
        
        // 无网络
    }else{
        
        
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"网络异常,请检查网络连接" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
                [alertView release];
        
    }
    
    
}





//设置nav的字体颜色
- (void)setNav{
    
    UINavigationBar *bar = [UINavigationBar appearance];
    
    bar.tintColor = [UIColor whiteColor];
    
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    
   NSArray *arr = [[DbHandel shareDbHandel] selectDengLuTable];
    
    for (DengLuTabelModel *model in arr) {
        [[DbHandel shareDbHandel] deleteDengLuModel:model];
    }
    
}

@end
